
package jermedit.gobjekte;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.io.Serializable;

/**
 * LineObject
 * 
 * Adding Points to the Lines can be done here. However displaying them must be 
 * done in the Sub Classes because there is where the Lines are displayed
 * we can display EditPoint objects here.
 * => Set Stroke Types in the Subclass, Draw all lines here, Draw Arrows
 * Text Names ect in Subclasses.
 * Subclasses are drawing good Lines at the end. We shold find a way 
 * to draw sections from Point to Point
 * => one Method to set Stroke fro9m Subclass then draw Sections from here 
 * then Draw Endline from Subclass
 * 
 * Remove double y,x 2 point coordinates
 * 
 * @author case
 */
public abstract class LineObject implements Serializable, Saveable {
       
       public boolean selected = false;   //Sellected Indikator
       private EditPoint firstEP, lastEP; // with public int x,y
       private int vonZOindex = -1; // Old Def -1
       private int nachZOindex = -1;// Old Def -1
       //private EditPoint ep = null;
       private String name = "";
       private String relation = "";
       
       public LineObject(int vonX, int vonY, int nachX, int nachY){
            firstEP = new EditPoint (vonX, vonY );
            lastEP = new EditPoint (nachX , nachY);
       }
       
       public LineObject(int vonX, int vonY, int nachX, int nachY, int vonZOindex, int nachZOindex, String name, String relation){
            firstEP = new EditPoint (vonX, vonY );
            lastEP = new EditPoint (nachX , nachY);
            this.vonZOindex = vonZOindex;
            this.nachZOindex = nachZOindex;
            this.name = name;
            this.relation = relation;
       }

        public abstract void drawSubStroke(Graphics2D g2);
	public abstract void drawSub(Graphics2D g2);
        
        /**
         * Diese Methode hilft uns gezeichnete und ausgewählte Objekte verändert zu Zeichnen
         * 
         * @param g2
         */
        public void draw(Graphics2D g2){
            // Falls ausgewählt
            if (this.selected){
               g2.setColor(Color.blue);               
               // Zeichnet das Objekt vom Punkt "von" zum Punkt "nach"
               g2.draw(firstEP);
               g2.draw(lastEP);
//               if (ep!=null){
//                    g2.draw(ep);
//               }
            }

            this.drawSubStroke(g2);
            // -> Draw All the Line Segments, Then Draw Last Segment
            // Draw Line between vonEP to next EP
            
            this.drawSub(g2);
            // Falls ausgewählt, sollen alle bisher gesetzten Sachen neu gesetzt werden
            if (this.selected){
                g2.setPaint(Color.black);
            }
        }
        
        
        public boolean isSelected() {
            return selected;
        }

        public boolean isInArea(int x, int y){
            int i = (int) Line2D.ptLineDist(this.getVonX(),this.getVonY(),this.getNachX(),this.getNachY(), x, y);
            if (i<2){
                return true;
            }
            return false;
        }
        
        /**
         * Setzt einen neuen Punkt für diese LineObject
         * für Anfang (point==1), oder Ende (point==2).
         * Soll nur für Instanzen von Geraden möglich sein!
         * @param point
         * @param x
         * @param y 
         */
        public void setPointx(int point, int x, int y){
            switch (point){
                case 1:
//                     this.firstEP.x = x;
//                     this.firstEP.y = y;
                     this.firstEP.move(x,y);
                     break;
                case 2:
//                     this.lastEP.x = x;
//                     this.lastEP.y = y;
                     this.lastEP.move(x,y);
                     break;
            }
        }

        public void setVonZOindex(int vonZOindex) {
            this.vonZOindex = vonZOindex;
        }
        
        public void setZOindexX(int x, int xZOindex) {
            switch (x){
                case 1:
                    this.vonZOindex = xZOindex;
                    break;
                case 2:
                    this.nachZOindex = xZOindex;
                    break;
            }
        }

        public void setNachZOindex(int nachZOindex) {
            this.nachZOindex = nachZOindex;
        }

        public void setSelected(boolean b) {
            this.selected = b;
        }

        public void setRelation(String relation) {
            this.relation = relation;
        }

        public void setName(String name) {
            this.name = name;
        }
        
        public int getVonZOindex() {
            return vonZOindex;
        }
        
        public int getNachZOindex() {
            return nachZOindex;
        }

        public String getName() {
            return name;
        }

        public String getRelation() {
            return relation;
        }    

        public int getVonX() {
            return firstEP.getMidX();
        }

        public int getVonY() {
            return firstEP.getMidY();
        }

        public int getNachX() {
            return lastEP.getMidX();
        }

        public int getNachY() {
            return lastEP.getMidY();
        }

        public EditPoint getVonEP() {
            return firstEP;
        }

        public EditPoint getNachEP() {
            return lastEP;
        }

        public String getObjectInString(){
            return "gr:"+ this.getClass().getSimpleName() + ";" +  firstEP.x + ";" + firstEP.y + ";" +  lastEP.x +  ";" + lastEP.y + ";" + vonZOindex + ";" +  nachZOindex + ";" +  name + ";" +  relation + ";" ;
        }
}
